﻿/*
Copyright (c) 2012 Antoine Reux

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "map.h"

void InitMap(struct s_map *_map)
{
	_map->n_tile=_map->width=_map->height=_map->offsetX=_map->offsetY=_map->chipsetOffsetY=0;
	_map->tile_sel=0;
	_map->chipset=NULL;
	_map->surf_chipset=NULL;
}

void RandomizeMap(struct s_map *_map, int width, int height, const char *chipset)
{
	int i,x,y;

	InitMap(_map);

	_map->surf_chipset=SDL_LoadBMP(chipset);
	SDL_SetColorKey(_map->surf_chipset, SDL_SRCCOLORKEY, SDL_MapRGB(_map->surf_chipset->format,8,33,82));

	_map->n_tile=(_map->surf_chipset->w/TILE_WIDTH)*(_map->surf_chipset->h/TILE_HEIGHT);
	_map->chipset = (struct s_chipset*)calloc((_map->n_tile),sizeof(struct s_chipset));

	x=y=0;

	for(i=0;i<_map->n_tile;i++)
	{

		_map->chipset[i].x=x;
		_map->chipset[i].y=y;
		_map->chipset[i].collision=1;
		_map->chipset[i].nextTile=0;

		x += TILE_WIDTH;
		if(x>(_map->surf_chipset->w-TILE_WIDTH))
		{
			x=0;
			y += TILE_HEIGHT;
		}
	}

	_map->width = width;
	_map->height = height;

	_map->data = (unsigned int*)malloc(_map->width*_map->height*sizeof(unsigned int));

	for(x=0;x<_map->width;x++)
		for(y=0;y<_map->height;y++)
			_map->data[x+y*_map->width]=rand()%(_map->n_tile);
}

void HandleMap(struct s_map *_map, int left)
{
	int x,y, tx,ty, i;
	SDL_Rect src, dest;

	SDL_FillRect(SDL_GetVideoSurface(), NULL, 0);

	x=-(_map->offsetX%TILE_WIDTH)+left;
	y=-(_map->offsetY%TILE_HEIGHT);
	tx=_map->offsetX/TILE_WIDTH;
	ty=_map->offsetY/TILE_HEIGHT;

	while(y<600)
	{
		x=-(_map->offsetX%TILE_WIDTH)+left;
		tx=_map->offsetX/TILE_WIDTH;
		while(x<800)
		{
			src.x=_map->chipset[GET_TILE(tx,ty,_map)].x;
			src.y=_map->chipset[GET_TILE(tx,ty,_map)].y;
			dest.x=x;
			dest.y=y;
			src.w=dest.w=TILE_WIDTH;
			src.h=dest.h=TILE_HEIGHT;

			if(dest.x<left)
			{
				src.x-=dest.x-left;
				src.w+=dest.x-left;
				dest.w+=dest.x-left;
				dest.x=left;
			}
			if(dest.y<0)
			{
				src.y-=dest.y;
				src.h+=dest.y;
				dest.h+=dest.h;
				dest.y=0;
			} 

			SDL_BlitSurface(_map->surf_chipset,&src,SDL_GetVideoSurface(),&dest);

			x+=TILE_WIDTH;
			tx++;
		}
		y+=TILE_HEIGHT;
		ty++;
	}

	/* Display chipset */
	x=1;
	y=1;
	for(i=_map->chipsetOffsetY;i<_map->n_tile;i++)
	{
		src.x=_map->chipset[i].x;
		src.y=_map->chipset[i].y;
		dest.x=x;
		dest.y=y;
		src.w=dest.w=TILE_WIDTH;
		src.h=dest.h=TILE_HEIGHT;

		if(i==_map->tile_sel)
		{
			dest.x--;
			dest.y--;
			dest.w+=2;
			dest.h+=2;
			SDL_FillRect(SDL_GetVideoSurface(), &dest, 
				(_map->chipset[i].collision==0)?
				SDL_MapRGB(SDL_GetVideoSurface()->format, 0,255,0)
				: SDL_MapRGB(SDL_GetVideoSurface()->format, 255,0,0));
			dest.x++;
			dest.y++;
			dest.w-=2;
			dest.h-=2;
		}

		SDL_BlitSurface(_map->surf_chipset,&src,SDL_GetVideoSurface(),&dest);

		dest.x+=TILE_WIDTH-2;
		dest.y+=TILE_HEIGHT-2;
		dest.w=dest.h=2;
		SDL_FillRect(SDL_GetVideoSurface(), &dest, 
			(_map->chipset[i].collision==0)?
			SDL_MapRGB(SDL_GetVideoSurface()->format, 0,255,0)
			: SDL_MapRGB(SDL_GetVideoSurface()->format, 255,0,0));


		x += TILE_WIDTH+1;
		if(x>(left-TILE_WIDTH-BORDER_SIZE))
		{
			x=1;
			y+=TILE_HEIGHT+1;
		}
	}
}

void MapEvent(SDL_Event *event, struct s_map *_map, int left)
{
	FILE *f;
	int i,j, ntileparcol;
	if(!event) return;

	ntileparcol=(left-BORDER_SIZE)/(TILE_WIDTH+1);

	switch(event->type)
	{
	case SDL_KEYDOWN:
		if(event->key.state==SDL_PRESSED)
		{
			if(event->key.keysym.sym==SDLK_RIGHT)
			{
				if (_map->offsetX<(_map->width*TILE_WIDTH-800)) _map->offsetX++;
			}
			else if(event->key.keysym.sym==SDLK_LEFT)
			{
				if (_map->offsetX!=0) _map->offsetX--;
			}
			else if(event->key.keysym.sym==SDLK_DOWN)
			{
				if (_map->offsetY<(_map->height*TILE_HEIGHT-600)) _map->offsetY++;
			}
			else if(event->key.keysym.sym==SDLK_UP)
			{
				if (_map->offsetY!=0) _map->offsetY--;
			}
			/* Chipset */
			else if(event->key.keysym.sym==SDLK_PAGEUP)
			{
				if (_map->chipsetOffsetY>=ntileparcol) _map->chipsetOffsetY-=ntileparcol;
			}
			else if(event->key.keysym.sym==SDLK_PAGEDOWN)
			{
				if (_map->chipsetOffsetY<_map->n_tile-ntileparcol) _map->chipsetOffsetY+=ntileparcol;
			}
			/* Custom functions */
			else if(event->key.keysym.sym==SDLK_f) /* Fill */
			{
				for(i=0;i<_map->width;i++)
					for(j=0;j<_map->height;j++)
						GET_TILE(i,j,_map)=_map->tile_sel;
			}
			else if(event->key.keysym.sym==SDLK_s) /* Save */
			{
				SaveMap(_map, "save.map", "mchip0.bmp");
			}
			else if(event->key.keysym.sym==SDLK_l) /* Load */
			{
				f=fopen("save.map","r");
				if(f)
				{
					fclose(f);
					FreeMap(_map);
					InitMap(_map);
					LoadMap(_map, "save.map");
				}
			}
		}
		break;

	case SDL_MOUSEBUTTONDOWN:
		if(event->button.button==SDL_BUTTON_LEFT)
		{
			if(event->button.x<=(left-BORDER_SIZE))
			{
				_map->tile_sel=_map->chipsetOffsetY+(event->button.x/(TILE_WIDTH+1))+ntileparcol*(event->button.y/(TILE_HEIGHT+1));
				if(_map->tile_sel>=_map->n_tile) _map->tile_sel=_map->n_tile-1;
			} else if(event->button.x>=left) {
				i=_map->offsetX+event->button.x-left;
				j=_map->offsetY+event->button.y;
				i /= TILE_WIDTH;
				j /= TILE_HEIGHT;

				if(i>=_map->width) i=_map->width-1;
				if(j>=_map->height) j=_map->height-1;
				GET_TILE(i,j,_map)=_map->tile_sel;

				/* When pressing CTRL */
				if((SDL_GetModState() & KMOD_CTRL) || (SDL_GetModState() & KMOD_ALT))
				{
					if(i<(_map->width-1)) GET_TILE(i+1,j,_map)=_map->tile_sel;
					if(j>0) GET_TILE(i,j-1,_map)=_map->tile_sel;
					if((j>0) && (i<(_map->width-1))) GET_TILE(i+1,j-1,_map)=_map->tile_sel;

					if(SDL_GetModState() & KMOD_ALT)
					{
						if(i>0) GET_TILE(i-1,j,_map)=_map->tile_sel;
						if(j<(_map->height-1)) GET_TILE(i,j+1,_map)=_map->tile_sel;

						if((i>0) && (j<(_map->height-1))) GET_TILE(i-1,j+1,_map)=_map->tile_sel;
						if((i>0) && (j>0)) GET_TILE(i-1,j-1,_map)=_map->tile_sel;
						if((j<(_map->height-1)) && (i<(_map->width-1))) GET_TILE(i+1,j+1,_map)=_map->tile_sel;
					}
				}
			}
		} else if(event->button.button==SDL_BUTTON_RIGHT)
		{
			if(event->button.x<=(left-BORDER_SIZE))
			{
				i=_map->chipsetOffsetY+(event->button.x/(TILE_WIDTH+1))+ntileparcol*(event->button.y/(TILE_HEIGHT+1));
				if(i>=_map->n_tile) i=_map->n_tile-1;
				_map->chipset[i].collision=(_map->chipset[i].collision+1)%2;
			}
		}
		break;
	}
}

void FreeMap(struct s_map *_map)
{
	SDL_FreeSurface(_map->surf_chipset);
	free(_map->data);
	free(_map->chipset);
}


void LoadMap(struct s_map *_map, char *filename)
{
	FILE *f;
	int i,x,y;
	char buffer[256];

	if(!_map) return;
	f=fopen(filename,"rb");
	if(!f) return;

	InitMap(_map);

	/* CHIPSET */
	memset(&buffer,0,sizeof(buffer));
	fread(&buffer,255,sizeof(char),f);
	_map->surf_chipset=SDL_LoadBMP(buffer);
	SDL_SetColorKey(_map->surf_chipset, SDL_SRCCOLORKEY, SDL_MapRGB(_map->surf_chipset->format,8,33,82));

	fread(&_map->n_tile,1,sizeof(unsigned int),f);
	fread(&_map->width,1,sizeof(unsigned int),f);
	fread(&_map->height,1,sizeof(unsigned int),f);
	fread(&_map->offsetX,1,sizeof(unsigned int),f);
	fread(&_map->offsetY,1,sizeof(unsigned int),f);

	_map->chipset = (struct s_chipset*)malloc((_map->n_tile)*sizeof(struct s_chipset));

	for(i=0;i<_map->n_tile;i++)
	{
		fread(&_map->chipset[i].x, 1, sizeof(unsigned int),f);
		fread(&_map->chipset[i].y, 1, sizeof(unsigned int),f);
		fread(&_map->chipset[i].collision, 1, sizeof(unsigned char),f);
		fread(&_map->chipset[i].nextTile, 1, sizeof(unsigned int),f);
	}

	_map->data = (unsigned int*)calloc(_map->width*_map->height,sizeof(unsigned int));

	for(x=0;x<_map->width;x++)
		for(y=0;y<_map->height;y++)
		{
			fread(&GET_TILE(x,y,_map), 1, sizeof(unsigned int),f);
			if(GET_TILE(x,y,_map)>=_map->n_tile)
			{
				fprintf(stderr,"File corrupted! (%d,%d)\n",x,y);
				GET_TILE(x,y,_map)=0;
			}
		}
		fclose(f);
}

void SaveMap(struct s_map *_map, char *filename, char *chipset)
{
	FILE *f;
	int i,x,y;
	char buffer[256];

	if(!_map) return;
	f=fopen(filename,"wb");
	if(!f) return;

	memset(&buffer,0,sizeof(buffer));
	strcpy(buffer,chipset);
	fwrite(&buffer,255,sizeof(char),f);

	fwrite(&_map->n_tile,1,sizeof(unsigned int),f);
	fwrite(&_map->width,1,sizeof(unsigned int),f);
	fwrite(&_map->height,1,sizeof(unsigned int),f);
	fwrite(&_map->offsetX,1,sizeof(unsigned int),f);
	fwrite(&_map->offsetY,1,sizeof(unsigned int),f);

	for(i=0;i<_map->n_tile;i++)
	{
		fwrite(&_map->chipset[i].x, 1, sizeof(unsigned int),f);
		fwrite(&_map->chipset[i].y, 1, sizeof(unsigned int),f);
		fwrite(&_map->chipset[i].collision, 1, sizeof(unsigned char),f);
		fwrite(&_map->chipset[i].nextTile, 1, sizeof(unsigned int),f);
	}

	for(x=0;x<_map->width;x++)
		for(y=0;y<_map->height;y++)
			fwrite(&GET_TILE(x,y,_map), 1, sizeof(unsigned int),f);
	fclose(f);
}